<template>
  <div class="my-tab-bar">
    <div
      class="tab-item"
      :class="{ current: tabIndex === i }"
      v-for="(tab, i) in tabList"
      :key="i"
      @click="changeTab(i, tab.componentName)"
    >
      <!-- 图标 -->
      <span class="iconfont" :class="tab.iconText"></span>
      <!-- 文字 -->
      <span>{{ tab.text }}</span>
    </div>
  </div>
</template>

<script>
export default {
  name: "MyTabBar",
  props: {
    // 接收 tab数据数组
    tabList: {
      type: Array, // 必须是数组
      required: true, // 必须传此参数
      validator(val) {
        // 自定义校验方法
        if (val.length > 1 && val.length <= 5) {
          return true;
        }
        console.error("tabList 元素个数 必须 >1 且 <=5 个哦~！");
        return false;
      },
    },
  },
  data() {
    return {
      tabIndex: 0,
    };
  },
  methods: {
    // 1.点击tab切换-------------------------------
    changeTab(i, cName) {
      this.tabIndex = i;
      this.$emit("onChangeTab", cName);
    },
  },
};
</script>

<style lang="less" scoped>
.my-tab-bar {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 50px;
  border-top: 1px solid #ccc;
  display: flex;
  justify-content: space-around;
  align-items: center;
  background-color: white;
  .tab-item {
    display: flex;
    flex-direction: column;
    align-items: center;
  }
}

.current {
  color: #1d7bff;
}
</style>